package com.example.administrator.szb.xzyw;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.LoginNewActivity;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.SQYWBean;
import com.example.administrator.szb.bean.YWBDBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Toasts;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/10/12.
 */

public class SBZRActivity extends BaseActivity {
    @Bind(R.id.tjzl_edit_gpdm_sbzr)
    EditText tjzlEditGpdmSbzr;
    @Bind(R.id.tjzl_ll_gpdm_sbzr)
    LinearLayout tjzlLlGpdmSbzr;
    @Bind(R.id.tjzl_edit_crgs_sbzr)
    EditText tjzlEditCrgsSbzr;
    @Bind(R.id.tjzl_ll_crgs_sbzr)
    LinearLayout tjzlLlCrgsSbzr;
    @Bind(R.id.tjzl_edit_crjg_sbzr)
    EditText tjzlEditCrjgSbzr;
    @Bind(R.id.tjzl_ll_crjg_sbzr)
    LinearLayout tjzlLlCrjgSbzr;
    @Bind(R.id.textView73)
    TextView textView73;
    @Bind(R.id.tjzl_edit_pe_sbzr)
    EditText tjzlEditPeSbzr;
    @Bind(R.id.tjzl_ll_pe_sbzr)
    LinearLayout tjzlLlPeSbzr;
    @Bind(R.id.tjzl_text_zxcs_sbzr)
    TextView tjzlTextZxcsSbzr;
    @Bind(R.id.tjzl_ll_zxcs_sbzr)
    LinearLayout tjzlLlZxcsSbzr;
    @Bind(R.id.tjzl_text_ddlr_sbzr)
    TextView tjzlTextDdlrSbzr;
    @Bind(R.id.tjzl_ll_ddlr_sbzr)
    LinearLayout tjzlLlDdlrSbzr;
    @Bind(R.id.tjzl_text_scgh_sbzr)
    TextView tjzlTextScghSbzr;
    @Bind(R.id.tjzl_ll_scgh_sbzr)
    LinearLayout tjzlLlScghSbzr;
    @Bind(R.id.tjzl_edit_lxr_sbzr)
    EditText tjzlEditLxrSbzr;
    @Bind(R.id.tjzl_ll_lxr_sbzr)
    LinearLayout tjzlLlLxrSbzr;
    @Bind(R.id.tjzl_edit_lxfs_sbzr)
    EditText tjzlEditLxfsSbzr;
    @Bind(R.id.tjzl_ll_lxfs_sbzr)
    LinearLayout tjzlLlLxfsSbzr;
    @Bind(R.id.tjzl_edit_yq_sbzr)
    EditText tjzlEditYqSbzr;
    @Bind(R.id.tjzl_test_zs_sbzr)
    TextView tjzlTestZsSbzr;
    @Bind(R.id.tjzl_button_tj_sbzr)
    Button tjzlButtonTjSbzr;
    @Bind(R.id.tjzl_text_bmxy_sbzr)
    TextView tjzlTextBmxySbzr;

    private int counselor_id = 0;
    YWBDBean ywbdBean;
    ArrayList<String> info = new ArrayList<>();

    int position = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sbzr_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);
        initToolbar(R.id.sbzr_toolbar_include, "三板转让");

        requestData(getIntent().getIntExtra("id", -1));
        editListenter(tjzlEditYqSbzr, tjzlTestZsSbzr);//设置字数监听
        hasIntent();
    }
    private void hasIntent() {
        counselor_id = getIntent().getIntExtra("counselor_id",0);
    }

    private void requestData(int id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", id + "");
        map.put("counselor_id", counselor_id + "");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, YWBDBean.class, URLAddress.URLBASE + URLAddress.URL_BDYW, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ywbdBean = (YWBDBean) response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.tjzl_ll_zxcs_sbzr, R.id.tjzl_ll_ddlr_sbzr, R.id.tjzl_ll_scgh_sbzr, R.id.tjzl_button_tj_sbzr, R.id.tjzl_text_bmxy_sbzr})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tjzl_ll_zxcs_sbzr:
                yw_ci_show(1);
                break;
            case R.id.tjzl_ll_ddlr_sbzr:
                yw_ci_show(2);
                break;
            case R.id.tjzl_ll_scgh_sbzr:
                yw_ci_show(3);
                break;
            case R.id.tjzl_button_tj_sbzr:
                if (SampleApplicationLike.getUserloginInstance().getId()<=0) {
                    startActivity(new Intent(this, LoginNewActivity.class));
                }else{
                    requestSubmitData();
                }
                break;
            case R.id.tjzl_text_bmxy_sbzr:
                break;
        }
    }
    private void requestSubmitData() {
        boolean flag = checkInfo();
        if (flag) {
            doSubmit();
        } else {

        }
    }

    private void doSubmit() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", getIntent().getIntExtra("id", -1) + "");
        map.put("counselor_id", counselor_id + "");
        map.put("users_id", ""+SampleApplicationLike.getUserloginInstance().getId());//测试
        map.put("namelink", info.get(info.size() - 3) + "");
        map.put("tel", info.get(info.size() - 2) + "");
        map.put("content", info.get(info.size() - 1) + "");

        for (int i = 0; i < ywbdBean.getData().getBusField().size(); i++) {
            map.put("" + ywbdBean.getData().getBusField().get(i).getId(), info.get(i));
        }

        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, SQYWBean.class, URLAddress.URLBASE + URLAddress.URL_SUBMINT_BDYW, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                if (((SQYWBean) response).getResult() != 1) {
                    Toasts.show(SBZRActivity.this, "" + ((SQYWBean) response).getErr_msg(), Toast.LENGTH_SHORT);
                } else {
                    startActivity(new Intent(SBZRActivity.this, SuccessActivity.class));
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
            }
        });
    }

    private boolean checkInfo() {
        getInfo();
        return true;
    }
    private void getInfo() {
        info.clear();
        info.add(tjzlEditGpdmSbzr.getText().toString().trim());

        if (TextUtils.isEmpty(tjzlTextZxcsSbzr.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextZxcsSbzr.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }

        position++;
        if (TextUtils.isEmpty(tjzlTextDdlrSbzr.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextDdlrSbzr.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }

        position++;

        if (TextUtils.isEmpty(tjzlTextScghSbzr.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextScghSbzr.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }

        info.add(tjzlEditCrgsSbzr.getText().toString().trim());
        info.add(tjzlEditCrjgSbzr.getText().toString().trim());
        info.add(tjzlEditPeSbzr.getText().toString().trim());

        info.add(tjzlEditLxrSbzr.getText().toString().trim());
        info.add(tjzlEditLxfsSbzr.getText().toString().trim());
        info.add(tjzlEditYqSbzr.getText().toString().trim());


    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void yw_ci_show(final int i) {
        if (ywbdBean != null) {
            BaseAdapter<YWBDBean.DataBean.BusFieldBean.BuschoicesBean> adapter = new BaseAdapter<YWBDBean.DataBean.BusFieldBean.BuschoicesBean>(this, ywbdBean.getData().getBusField().get(i).getBuschoices()) {
                @Override
                public void convert(int position, BaseViewHolder holder, YWBDBean.DataBean.BusFieldBean.BuschoicesBean bean) {
                    ((TextView) holder.getView(R.id.base_tjzl_popuwindow_text_item)).setText(bean.getTitle());
                }

                @Override
                public int getLayoutId(int viewType) {
                    return R.layout.view_popuwindow_base_tjzl_item;
                }
            };
            showPopupwindow(R.layout.view_popuwindow_base_tjzl, R.layout.ggpz_activity, adapter);
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    popWindow.dissmiss();
                    String info_view = ((TextView) view.findViewById(R.id.base_tjzl_popuwindow_text_item)).getText().toString();
                    int id = -1;
                   /* for (BusinessBean.DataBean b : WecomeActivity.businessBean.getData()) {
                        if (info_view.equals(b.getTitle())) {
                            id = b.getId();
                        }
                    }*/
                    switch (i) {
                        case 1:
                            tjzlTextZxcsSbzr.setText("" + info_view);
                            break;
                        case 2:
                            tjzlTextDdlrSbzr.setText("" + info_view);
                            break;
                        case 3:
                            tjzlTextScghSbzr.setText("" + info_view);
                            break;
                    }

                }
            });
        }
    }
}
